<?php


namespace app\agent\controller\agent;
use app\agent\controller\Controller;
use app\agent\model\agent\AgentWxapp as AgentWxappModel;
use think\Session;

class Routine extends Controller{
    /**
     * 小程序配置首页
     */
    public function index(){
        $session = Session::get('yoshop_agent');
        $agent_id = $session['agent']['agent_id'];

        $model = AgentWxappModel::getInfo($agent_id);
        
        if (!$this->request->isAjax()) {
            return $this->fetch('index', ['model'=>$model]);
        }

        if(empty($model)){
            $models = new AgentWxappModel();
            // 更新小程序设置
            if ($models->add($this->postData('wxapp'),$agent_id)) {
                return $this->renderSuccess('更新成功');
            }
        }else{
            // 更新小程序设置
            if ($model->edit($this->postData('wxapp'),$agent_id)) {
                return $this->renderSuccess('更新成功');
            }
        }


        return $this->renderError( '更新失败');
    }
}